#include "personal_info_ui.h"
#include <QIcon>
#include <QPixmap>
#include "UserPersonal_ui_logic.h"
#include"globals.h"
PersonalInfoUI::PersonalInfoUI(QWidget *parent)
    : QWidget(parent)
{
    setupUi();

}

void PersonalInfoUI::setupUi()
{
    this->setFixedSize(1680, 930);

    // Disable maximize and minimize buttons
    this->setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
    this->setAttribute(Qt::WA_StyledBackground, true);
    this->setWindowTitle(tr("PiliPili_Buy"));
    this->setWindowIcon(QIcon(QString::fromUtf8(":/icon/logo.png")));
    QString styleSheet = QString("background-color: rgb(173, 216, 230);");
    this->setStyleSheet(styleSheet);

    avatarLabel = new QLabel(this);
    QPixmap avatarPixmap(":/icon/logo.png");
    QPixmap scaledImage = avatarPixmap.scaled(100,
                                              150,
                                              Qt::KeepAspectRatio,
                                              Qt::SmoothTransformation);

    // 设置头像图片路径
    avatarLabel->setPixmap(scaledImage);
    avatarLabel->setFixedSize(100, 100); // 设置头像大小
    avatarLabel->setFrameStyle(QFrame::Box | QFrame::Raised);
    avatarLabel->setAlignment(Qt::AlignCenter);

    // 确保QLabel的边距为0，这样图片可以紧贴边框
    avatarLabel->setContentsMargins(0, 0, 0, 0);

    QLabel *nicknameLabel = new QLabel(tr("昵称:"));
    nicknameEdit = new QLineEdit;
    QLabel *genderLabel = new QLabel(tr("性别:"));
    genderEdit = new QLineEdit;
    QLabel *emailLabel = new QLabel(tr("邮箱:"));
    emailEdit = new QLineEdit;
    QLabel *signatureLabel = new QLabel(tr("个性签名:"));
    signatureEdit = new QLineEdit;

    save = new QPushButton(tr("保存"));
    accountSecurityButton = new QPushButton(tr("账户安全"));

    QHBoxLayout *avatarLayout = new QHBoxLayout;
    avatarLayout->addStretch();
    avatarLayout->addWidget(avatarLabel);
    avatarLayout->addStretch();

    QGridLayout *infoLayout = new QGridLayout;
    infoLayout->addWidget(nicknameLabel, 0, 0);
    infoLayout->addWidget(nicknameEdit, 0, 1);
    infoLayout->addWidget(genderLabel, 1, 0);
    infoLayout->addWidget(genderEdit, 1, 1);
    infoLayout->addWidget(emailLabel, 2, 0);
    infoLayout->addWidget(emailEdit, 2, 1);
    infoLayout->addWidget(signatureLabel, 3, 0);
    infoLayout->addWidget(signatureEdit, 3, 1);

    infoLayout->setRowStretch(4, 1);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(avatarLayout);
    mainLayout->addLayout(infoLayout);
    mainLayout->addWidget(save);
    mainLayout->addWidget(accountSecurityButton);
    mainLayout->addStretch();

    setLayout(mainLayout);

}
void PersonalInfoUI::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && avatarLabel->geometry().contains(event->pos()))
    {
        emit avatarLabelClicked(); // 只有点击 avatarLabel 时才发送信号
    }
    QWidget::mousePressEvent(event); // 调用基类的事件处理
}

PersonalInfoUI::~PersonalInfoUI() {}

QLineEdit *PersonalInfoUI::getNicknameEdit() const
{
    return nicknameEdit;
}
QLineEdit *PersonalInfoUI::getGenderEdit() const
{
    return genderEdit;
}
QLineEdit *PersonalInfoUI::getBirthdayEdit() const
{
    return birthdayEdit;
}
QLineEdit *PersonalInfoUI::getEmailEdit() const
{
    return emailEdit;
}
QLineEdit *PersonalInfoUI::getSignatureEdit() const
{
    return signatureEdit;
}
QPushButton *PersonalInfoUI::getSaveButton() const
{
    return save;
}
QPushButton *PersonalInfoUI::getAccountSecurityButton() const
{
    return accountSecurityButton;
}
void PersonalInfoUI::closeEvent(QCloseEvent *event)
{
    // Emit signal when the window is closed
    emit windowClosed();
    event->accept();  // Allow the window to close

}
